﻿/*----------------------------------------------------------------
// Copyright (C) 2012
// 版权所有
//
// 文件名：OrderOnline_Right.cs
// 文件功能描述：
//
//
// 创建标识：Libing 2012-05-07 13:23:03 星期一
//
// 修改标识：
// 修改描述：
//----------------------------------------------------------------*/

using System;
using System.Collections.Generic;
using System.Text;

using System.Data;
using System.Data.SqlClient;

using WMS.DBUtility;

namespace WMS.SQLServerDAL
{
    public class OrderOnline_Right : WMS.IDAL.IOrderOnline_Right
    {
        #region 基本方法

        /// <summary>
        /// 添加实体
        /// </summary>
        /// <param name="orderOnlineRight">类型为 WMS.Model.OrderOnline_Right 的实体</param>
        /// <returns></returns>
        public bool Insert(WMS.Model.OrderOnline_Right orderOnlineRight)
        {
            SqlParameter[] parameters = {
				new SqlParameter("@ID", SqlDbType.VarChar, 50),
				new SqlParameter("@RightName", SqlDbType.NVarChar, 50),
				new SqlParameter("@PagePath", SqlDbType.NVarChar, -1),
				new SqlParameter("@RightDescr", SqlDbType.NText),
				new SqlParameter("@ModelID", SqlDbType.VarChar, 50),
				new SqlParameter("@RightType", SqlDbType.Int, 4),
				new SqlParameter("@Sno", SqlDbType.Int, 4)
			};
            parameters[0].Value = orderOnlineRight.ID;
            parameters[1].Value = orderOnlineRight.RightName;
            parameters[2].Value = orderOnlineRight.PagePath;
            parameters[3].Value = orderOnlineRight.RightDescr;
            parameters[4].Value = orderOnlineRight.ModelID;
            parameters[5].Value = orderOnlineRight.RightType;
            parameters[6].Value = orderOnlineRight.Sno;

            try
            {
                int result = DbHelperSQL.ExecuteStoredProcNonQuery("[OrderOnline_Right_Insert]", parameters);

                return result > 0;
            }
            catch (SqlException ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }

        /// <summary>
        /// 修改实体
        /// </summary>
        /// <param name="orderOnlineRight">类型为 WMS.Model.OrderOnline_Right 的实体</param>
        /// <returns></returns>
        public bool Update(WMS.Model.OrderOnline_Right orderOnlineRight)
        {
            SqlParameter[] parameters = {
				new SqlParameter("@ID", SqlDbType.VarChar, 50),
				new SqlParameter("@RightName", SqlDbType.NVarChar, 50),
				new SqlParameter("@PagePath", SqlDbType.NVarChar, -1),
				new SqlParameter("@RightDescr", SqlDbType.NText),
				new SqlParameter("@ModelID", SqlDbType.VarChar, 50),
				new SqlParameter("@RightType", SqlDbType.Int, 4),
				new SqlParameter("@Sno", SqlDbType.Int, 4)
			};
            parameters[0].Value = orderOnlineRight.ID;
            parameters[1].Value = orderOnlineRight.RightName;
            parameters[2].Value = orderOnlineRight.PagePath;
            parameters[3].Value = orderOnlineRight.RightDescr;
            parameters[4].Value = orderOnlineRight.ModelID;
            parameters[5].Value = orderOnlineRight.RightType;
            parameters[6].Value = orderOnlineRight.Sno;

            try
            {
                return DbHelperSQL.ExecuteStoredProcNonQuery("[OrderOnline_Right_Update]", parameters) > 0;
            }
            catch (SqlException ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }

        /// <summary>
        /// 最小修改实体
        /// </summary>
        /// <param name="model">类型为WMS.Model.OrderOnline_Right的实体</param>
        /// <returns></returns>
        public bool UpdateMinimal(WMS.Model.OrderOnline_Right orderOnlineRight)
        {
            SqlCommand cmd = new SqlCommand("[OrderOnline_Right_UpdateMinimal]");

            if (orderOnlineRight.ID != string.Empty)
            {
                cmd.Parameters.Add(new SqlParameter("@ID", SqlDbType.VarChar, 50)).Value = orderOnlineRight.ID;
            }

            if (orderOnlineRight.RightName != string.Empty)
            {
                cmd.Parameters.Add(new SqlParameter("@RightName", SqlDbType.NVarChar, 50)).Value = orderOnlineRight.RightName;
            }

            if (orderOnlineRight.PagePath != string.Empty)
            {
                cmd.Parameters.Add(new SqlParameter("@PagePath", SqlDbType.NVarChar, -1)).Value = orderOnlineRight.PagePath;
            }

            if (orderOnlineRight.RightDescr != string.Empty)
            {
                cmd.Parameters.Add(new SqlParameter("@RightDescr", SqlDbType.NText)).Value = orderOnlineRight.RightDescr;
            }

            if (orderOnlineRight.ModelID != string.Empty)
            {
                cmd.Parameters.Add(new SqlParameter("@ModelID", SqlDbType.VarChar, 50)).Value = orderOnlineRight.ModelID;
            }

            if (orderOnlineRight.RightType != null)
            {
                cmd.Parameters.Add(new SqlParameter("@RightType", SqlDbType.Int, 4)).Value = orderOnlineRight.RightType;
            }

            if (orderOnlineRight.Sno != null)
            {
                cmd.Parameters.Add(new SqlParameter("@Sno", SqlDbType.Int, 4)).Value = orderOnlineRight.Sno;
            }

            try
            {
                return DbHelperSQL.ExecuteStoredProcNonQuery(cmd) > 0;
            }
            catch (SqlException ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }

        /// <summary>
        /// 删除实体(根据主键 ID)
        /// </summary>
        /// <param name="id">ID</param>
        /// <returns></returns>
        public bool DeleteByID(string id)
        {
            SqlParameter[] parameters = {
				new SqlParameter("@ID", SqlDbType.VarChar,50)
			};
            parameters[0].Value = id;

            try
            {
                return DbHelperSQL.ExecuteStoredProcNonQuery("[OrderOnline_Right_DeleteByID]", parameters) > 0;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }

        /// <summary>
        /// 删除实体(指定条件)
        /// </summary>
        /// <param name="whereClause">WHERE语句</param>
        /// <returns></returns>
        public bool DeleteDynamic(string whereClause)
        {
            SqlParameter[] parameters = {
				new SqlParameter("@WhereClause", SqlDbType.NVarChar,2000)
			};
            parameters[0].Value = whereClause;

            try
            {
                return DbHelperSQL.ExecuteStoredProcNonQuery("[OrderOnline_Right_DeleteDynamic]", parameters) > 0;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }

        /// <summary>
        /// 获取实体(根据主键 ID)
        /// </summary>
        /// <param name="id">ID</param>
        /// <returns></returns>
        public WMS.Model.OrderOnline_Right GetByID(string id)
        {
            SqlParameter[] parameters = {
				new SqlParameter("@ID", SqlDbType.VarChar,50)
			};
            parameters[0].Value = id;

            try
            {
                List<WMS.Model.OrderOnline_Right> orderOnlineRightList = new List<WMS.Model.OrderOnline_Right>();

                SqlDataReader dr = DbHelperSQL.ExecuteStoredProcReader("[OrderOnline_Right_GetByID]", parameters);
                while (dr.Read())
                {
                    WMS.Model.OrderOnline_Right orderOnlineRight = new WMS.Model.OrderOnline_Right();

                    orderOnlineRight.ID = Convert.ToString(dr["ID"]);
                    orderOnlineRight.RightName = Convert.ToString(dr["RightName"]);
                    orderOnlineRight.PagePath = Convert.ToString(dr["PagePath"]);
                    orderOnlineRight.RightDescr = Convert.ToString(dr["RightDescr"]);
                    orderOnlineRight.ModelID = Convert.ToString(dr["ModelID"]);
                    orderOnlineRight.RightType = Convert.ToInt32(dr["RightType"]);
                    orderOnlineRight.Sno = Convert.ToInt32(dr["Sno"]);
                    orderOnlineRight.ModelState = WMS.Model.Enum.ModelState.None;

                    orderOnlineRightList.Add(orderOnlineRight);
                }

                // Close the dr, which will cause the connection to be closed as well.
                dr.Close();

                if (orderOnlineRightList.Count > 0)
                {
                    return orderOnlineRightList[0];
                }
                else
                {
                    return null;
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }

        #endregion

        #region	公共方法

        /// <summary>
        /// 获取实体列表(指定数目和查询条件)
        /// </summary>
        /// <param name="topClause">指定实体数</param>
        /// <param name="whereClause">WHERE语句</param>
        /// <returns></returns>
        public List<WMS.Model.OrderOnline_Right> Get(int topClause, string whereClause)
        {
            SqlParameter[] parameters = {
				new SqlParameter("@TopClause", SqlDbType.Int,4),
				new SqlParameter("@WhereClause", SqlDbType.NVarChar,2000)
			};
            parameters[0].Value = topClause;
            parameters[1].Value = whereClause;

            try
            {
                List<WMS.Model.OrderOnline_Right> orderOnlineRightList = new List<WMS.Model.OrderOnline_Right>();

                SqlDataReader dr = DbHelperSQL.ExecuteStoredProcReader("[OrderOnline_Right_GetDynamic]", parameters);
                while (dr.Read())
                {
                    WMS.Model.OrderOnline_Right orderOnlineRight = new WMS.Model.OrderOnline_Right();

                    orderOnlineRight.ID = Convert.ToString(dr["ID"]);
                    orderOnlineRight.RightName = Convert.ToString(dr["RightName"]);
                    orderOnlineRight.PagePath = Convert.ToString(dr["PagePath"]);
                    orderOnlineRight.RightDescr = Convert.ToString(dr["RightDescr"]);
                    orderOnlineRight.ModelID = Convert.ToString(dr["ModelID"]);
                    orderOnlineRight.RightType = Convert.ToInt32(dr["RightType"]);
                    orderOnlineRight.Sno = Convert.ToInt32(dr["Sno"]);
                    orderOnlineRight.ModelState = WMS.Model.Enum.ModelState.None;

                    orderOnlineRightList.Add(orderOnlineRight);
                }

                // Close the dr, which will cause the connection to be closed as well.
                dr.Close();

                return orderOnlineRightList;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }

        /// <summary>
        /// 获取实体列表(指定查询条件)
        /// </summary>
        /// <param name="whereClause">WHERE语句</param>
        /// <returns></returns>
        public List<WMS.Model.OrderOnline_Right> Get(string whereClause)
        {
            return Get(0, whereClause);
        }

        /// <summary>
        /// 获取分页实体列表
        /// </summary>
        /// <param name="whereClause">WHERE 语句</param>
        /// <param name="orderByClause">ORDER BY 语句</param>
        /// <param name="pageIndex">分页索引</param>
        /// <param name="pageSize">分页大小(每页显示的记录数)</param>
        /// <param name="totalRecords">记录总数</param>
        /// <returns></returns>
        public List<WMS.Model.OrderOnline_Right> GetPaged(string whereClause, string orderBy, int pageIndex, int pageSize, out int totalRecords)
        {
            SqlParameter[] parameters = {
				new SqlParameter("@WhereClause", SqlDbType.NVarChar, 2000),
				new SqlParameter("@OrderBy", SqlDbType.NVarChar, 1000),
				new SqlParameter("@PageIndex", SqlDbType.Int, 4),
				new SqlParameter("@PageSize", SqlDbType.Int, 4)
			};
            parameters[0].Value = whereClause;
            parameters[1].Value = orderBy;
            parameters[2].Value = pageIndex;
            parameters[3].Value = pageSize;

            try
            {
                List<WMS.Model.OrderOnline_Right> orderOnlineRightList = new List<WMS.Model.OrderOnline_Right>();

                SqlDataReader dr = DbHelperSQL.ExecuteStoredProcReader("[OrderOnline_Right_GetPaged]", parameters);
                while (dr.Read())
                {
                    WMS.Model.OrderOnline_Right orderOnlineRight = new WMS.Model.OrderOnline_Right();

                    orderOnlineRight.ID = Convert.ToString(dr["ID"]);
                    orderOnlineRight.RightName = Convert.ToString(dr["RightName"]);
                    orderOnlineRight.PagePath = Convert.ToString(dr["PagePath"]);
                    orderOnlineRight.RightDescr = Convert.ToString(dr["RightDescr"]);
                    orderOnlineRight.ModelID = Convert.ToString(dr["ModelID"]);
                    orderOnlineRight.RightType = Convert.ToInt32(dr["RightType"]);
                    orderOnlineRight.Sno = Convert.ToInt32(dr["Sno"]);
                    orderOnlineRight.ModelState = WMS.Model.Enum.ModelState.None;

                    orderOnlineRightList.Add(orderOnlineRight);
                }

                totalRecords = orderOnlineRightList.Count;

                if (dr.NextResult())
                {
                    if (dr.Read())
                    {
                        totalRecords = Convert.ToInt32(dr[0]);
                    }
                }

                // Close the dr, which will cause the connection to be closed as well.
                dr.Close();

                return orderOnlineRightList;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }

        #endregion

        #region 自定义方法

        /// <summary>
        /// 根据ModuleID获取实体列表
        /// </summary>
        /// <param name="modelID">一级模块ID</param>
        /// <returns></returns>
        public List<WMS.Model.OrderOnline_Right> GetByModelID(string modelID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.AppendFormat("ModelID='{0}' ORDER BY Sno", modelID);

            return Get(strSql.ToString());
        }

        /// <summary>
        /// 根据跟节点获取完整树
        /// </summary>
        /// <param name="modelID">根节点ID</param>
        /// <returns></returns>
        public List<WMS.Model.MainMenuTree> GetAllChildrenByModelID(string modelID)
        {
            List<WMS.Model.MainMenuTree> mainMenuTree = new List<Model.MainMenuTree>();

            List<WMS.Model.OrderOnline_Right> orderOnlineRightList = GetByModelID(modelID);
            foreach (WMS.Model.OrderOnline_Right orderOnlineRight in orderOnlineRightList)
            {
                WMS.Model.MainMenuTree mainMenu = new Model.MainMenuTree();
                mainMenu.id = orderOnlineRight.ID;
                mainMenu.text = orderOnlineRight.RightName;
                mainMenu.attributes = orderOnlineRight.PagePath;

                mainMenuTree.Add(mainMenu);

                GetChildByModelID(mainMenu, mainMenu.id);
            }

            return mainMenuTree;
        }

        public void GetChildByModelID(WMS.Model.MainMenuTree parentMainMenu, string modelID)
        {
            List<WMS.Model.OrderOnline_Right> orderOnlineRightList = GetByModelID(modelID);
            foreach (WMS.Model.OrderOnline_Right orderOnlineRight in orderOnlineRightList)
            {
                WMS.Model.MainMenuTree mainMenu = new Model.MainMenuTree();
                mainMenu.id = orderOnlineRight.ID;
                mainMenu.text = orderOnlineRight.RightName;
                mainMenu.attributes = orderOnlineRight.PagePath;

                parentMainMenu.children.Add(mainMenu);

                // 递归调用，创建其他节点
                GetChildByModelID(mainMenu, mainMenu.id);
            }
        }

        #endregion
    }
}
